#include "atomic_0828_preprocessing_service.h"

int getDataPreprocess(DataPreprocessInput *input, DataPreprocessOutput *output) {
    // 校验输入输出指针有效性
    if (input == nullptr || output == nullptr) {
        if (output != nullptr) {
            output->is_valid = 0;
            output->return_type = ATOMIC_SERVICE_ERROR;
        }
        return ATOMIC_SERVICE_ERROR;
    }

    // 初始化输出结构体
    memset(output, 0, sizeof(DataPreprocessOutput));

    // 校验输入参数合法性
    if (input->img_width <= 0 || input->img_height <= 0) {
        output->is_valid = 0;
        output->return_type = ATOMIC_SERVICE_ERROR;
        return ATOMIC_SERVICE_ERROR;
    }

    if (input->pixel_count != input->img_width * input->img_height) {
        output->is_valid = 0;
        output->return_type = ATOMIC_SERVICE_ERROR;
        return ATOMIC_SERVICE_ERROR;
    }

    if (input->noise_level < 0 || input->noise_level > 10) {
        output->is_valid = 0;
        output->return_type = ATOMIC_SERVICE_ERROR;
        return ATOMIC_SERVICE_ERROR;
    }

    // 模拟预处理操作
    try {
        // 1. 去噪处理（模拟）：根据噪声等级调整输出尺寸（这里简单保持原尺寸）
        output->proc_width = input->img_width;
        output->proc_height = input->img_height;

        // 2. 特征提取：计算像素极值（根据噪声等级模拟）
        // 噪声等级越高，有效像素范围越窄
        output->pixel_min = input->noise_level * 10;
        output->pixel_max = 255 - input->noise_level * 10;

        // 确保极值在有效范围内
        if (output->pixel_min < 0) output->pixel_min = 0;
        if (output->pixel_max > 255) output->pixel_max = 255;
        if (output->pixel_min > output->pixel_max) {
            std::swap(output->pixel_min, output->pixel_max);
        }

        // 3. 设置处理结果有效性
        output->is_valid = 1;
        output->return_type = ATOMIC_SERVICE_SUCCESS;

        return ATOMIC_SERVICE_SUCCESS;
    } catch (...) {
        // 捕获所有异常，确保程序稳定性
        output->is_valid = 0;
        output->return_type = ATOMIC_SERVICE_ERROR;
        return ATOMIC_SERVICE_ERROR;
    }
}